package gui.userinput;

import gui.userslist.UserSelectListener;

import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import network.SendSignalException;

import controller.MessageController;
import controller.UserNotConnected;

import ui.StateObserver;
import user.LocalUser;
import user.User;
import utils.Utils;
import utils.Utils.Extracter;

public class ChatInput extends JPanel implements UserSelectListener, StateObserver {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JLabel labelUser ;
	private JTextField userInput ;
	private Set <User> selectedUsers = null ;

	public ChatInput (final MessageController msgCtrl) {
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)) ;
		this.labelUser = new JLabel() ;
		this.labelUser.setHorizontalAlignment(SwingConstants.LEFT) ;
		this.labelUser.setAlignmentX(LEFT_ALIGNMENT) ;
		this.labelUser.setMaximumSize(new Dimension(Short.MAX_VALUE, 50)) ;
		this.userInput = new JTextField() ;
		this.userInput.setEnabled(false) ;
		this.userInput.setAlignmentX(LEFT_ALIGNMENT) ;
		this.userInput.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)) ;
		add(this.labelUser) ;
		add(this.userInput) ;
		this.userInput.addKeyListener(new KeyAdapter() {		
			@Override
			public void keyPressed(KeyEvent arg0) {
				if (arg0.getKeyCode() == KeyEvent.VK_ENTER && !ChatInput.this.userInput.getText().isEmpty()) {
					try {
						msgCtrl.sendText(ChatInput.this.selectedUsers, ChatInput.this.userInput.getText()) ;
						ChatInput.this.userInput.setText("") ;
					} 
					catch (UserNotConnected e) {

					} 
					catch (SendSignalException e) {
						
					}
				}
			}
		}) ;
		setLabelInfo (null) ;
		setMinimumSize(new Dimension(0, 80)) ;
	}
	
	/* (non-Javadoc)
	 * @see javax.swing.JComponent#requestFocus()
	 */
	@Override
	public void requestFocus () {
		super.requestFocus() ;
		this.userInput.requestFocus() ;
	}
	
	@Override
	public void fireConnect() {
		this.userInput.setEnabled(true) ;
	}

	@Override
	public void fireDisconnect() {
		this.userInput.setEnabled(false) ;
	}
	
	/**
	 * Update label info according to user names specified
	 * 
	 * @param usernames The user names, or null for all user
	 */
	private void setLabelInfo (ArrayList <String> usernames) {
		String value ;
		if (usernames == null || usernames.isEmpty()) {
			value = "Dire à tout le monde : " ;
		}
		else {
			value = "Dire à " + Utils.join(usernames, ", ") + " : " ;
		}
		this.labelUser.setText("<html>&nbsp;<em>" + value + "</em></html>") ;
	}
	
	/* (non-Javadoc)
	 * @see gui.window.list.UserSelectListener#userSelected(java.util.ArrayList)
	 */
	@Override
	public void userSelected(Set <User> users) {
		if (users.isEmpty()) {
			this.selectedUsers = null ;
			setLabelInfo(null) ;
		}
		else {
			this.selectedUsers = new HashSet <User> (users) ;
			if (users.size() > 1) {
				users.remove(LocalUser.getLocalUser()) ;
			}
			setLabelInfo(Utils.<User, String> extract (users,
					new Extracter <User, String>() {
						@Override
						public String extract (User other) {
							return other.getUsername();
						}
					})) ;
		}
		requestFocus() ;
	}

}
